let layer, form
$(function () {
    layui.use(['layer', 'form'], function () {
        layer = layui.layer, form = layui.form
        form.on('select(province)', function (data) {
            loadCity(data.value)
        })
        form.on('select(city)', function (data) {
            loadArea(data.value)
        })
        // 编辑时加载城市和区县
        form.on('select(edit_province)', function (data) {
            loadEditCity(data.value)
        })
        form.on('select(edit_city)', function (data) {
            loadEditArea(data.value)
        })
        form.on('switch(switchTest)', function (data) {
            console.log(data.othis)
            isDefault = this.checked ? 1 : 0
        });
    })
    $('.width_1200 .address-manage').on('mouseenter', '.address-item', function () {
        $(this).find('.model').fadeIn(100)
    })
    $('.width_1200 .address-manage').on('mouseleave', '.address-item', function () {
        $(this).find('.model').fadeOut(100)
    })
})

function addAddress() {
    layer.open({
        skin: 'demo-class',
        shade: [0.8, '#151b2d'],
        type: 1,
        anim: 5,
        area: ['40%', ''],
        content: $('#add-address').html(),
        resize: false,
        move: false,
        title: '添加收件人地址',
        yes: function (index, layero) {
            layer.close(index); //如果设定了yes回调，需进行手工关闭
        },
        success: function () {
            loadProvince()
            form.render()
        }
    });
}

/**
 * 加载省份
 */
function loadProvince() {
    $('#province').empty()
    post(api.AreaList, {level: 1}).then(res => {
        let option = '<option value="">--请选择--</option>'
        $('#province').append(option)
        $.each(res.data, function (index, item) {
            let option = '<option value="' + item.id + '">' + item.areaName + '</option>'
            $('#province').append(option)
        })
        form.render()
    })
}

/**
 * 加载城市
 * @param pid
 */
function loadCity(pid) {
    $('#city').empty()
    post(api.AreaChild, {parentId: pid, level: 2}).then(res => {
        let option = '<option value="">--请选择--</option>'
        $('#city').append(option)
        $.each(res.data, function (index, item) {
            let option = '<option value="' + item.id + '">' + item.areaName + '</option>'
            $('#city').append(option)
        })
        form.render()
    })
}

/**
 * 加载区县
 * @param pid
 */
function loadArea(pid) {
    $('#area').empty()
    post(api.AreaChild, {parentId: pid, level: 3}).then(res => {
        let option = '<option value="">--请选择--</option>'
        $('#area').append(option)
        $.each(res.data, function (index, item) {
            let option = '<option value="' + item.id + '">' + item.areaName + '</option>'
            $('#area').append(option)
        })
        form.render()
    })
}

/**
 * 保存地址
 */
function saveAddress() {
    let stuName = $("#stuName").val()
    let provinceId = $('#province').val()
    let cityId = $('#city').val()
    let areaId = $('#area').val()
    let addr = $('#addr').val()
    let stuMobile = $('#stuMobile').val()
    if (stuName == null || stuName == '') {
        layer.msg('收件人姓名不能为空')
        return
    }
    if (provinceId == null || provinceId == '') {
        layer.msg('所属省份不能为空')
        return
    }
    if (cityId == null || cityId == '') {
        layer.msg('所属城市不能为空')
        return
    }
    if (areaId == null || areaId == '') {
        layer.msg('所属区县不能为空')
        return
    }
    if (addr == null || addr == '') {
        layer.msg('详细地址不能为空')
        return
    }
    if (stuMobile == null || stuMobile == '') {
        layer.msg('手机不能为空')
        return
    }
    let address = {
        receiver: stuName,
        addr: addr,
        mobile: stuMobile,
        provinceId: provinceId,
        cityId: cityId,
        areaId: areaId,
    }
    post(api.SaveAddress, address).then(res => {
        location.reload()
    })
}

/**
 * 修改默认收货地址
 * @param id
 */
function changeDefault(id) {
    post(api.ChangeDefault, {id: id}).then(res => {
        location.href = api.OrderInfo + '?productId=' + $('#productId').val()
    })
}

/**
 * 编辑地址
 * @param id
 */
function editAddress(id) {
    layer.open({
        skin: 'demo-class',
        shade: [0.8, '#151b2d'],
        type: 1,
        anim: 5,
        area: ['40%', ''],
        content: $('#edit-address').html(),
        resize: false,
        move: false,
        title: '添加收件人地址',
        yes: function (index, layero) {
            layer.close(index); //如果设定了yes回调，需进行手工关闭
        },
        success: function () {
            post(api.AddressSingle, {id: id}).then(res => {
                $('#edit_stuName').val(res.data.receiver)
                $('#edit_addr').val(res.data.addr)
                $('#edit_stuMobile').val(res.data.mobile)
                $('#hidden_id').val(res.data.id)
            })
            loadEditProvince()
            form.render()
        }
    });
}

/**
 * 加载省份
 */
function loadEditProvince() {
    post(api.AreaList, {level: 1}).then(res => {
        let option = '<option value="">--请选择--</option>'
        $('#edit_province').append(option)
        $.each(res.data, function (index, item) {
            let option = '<option value="' + item.id + '">' + item.areaName + '</option>'
            $('#edit_province').append(option)
        })
        form.render()
    })
}

/**
 * 加载城市
 * @param pid
 */
function loadEditCity(pid) {
    $('#city').empty()
    post(api.AreaChild, {parentId: pid, level: 2}).then(res => {
        let option = '<option value="">--请选择--</option>'
        $('#edit_city').append(option)
        $.each(res.data, function (index, item) {
            let option = '<option value="' + item.id + '">' + item.areaName + '</option>'
            $('#edit_city').append(option)
        })
        form.render()
    })
}

/**
 * 加载区县
 * @param pid
 */
function loadEditArea(pid) {
    $('#area').empty()
    post(api.AreaChild, {parentId: pid, level: 3}).then(res => {
        let option = '<option value="">--请选择--</option>'
        $('#edit_area').append(option)
        $.each(res.data, function (index, item) {
            let option = '<option value="' + item.id + '">' + item.areaName + '</option>'
            $('#edit_area').append(option)
        })
        form.render()
    })
}

/**
 * 保存修改后的信息
 */
function saveUpdateAddress() {
    let stuName = $("#edit_stuName").val()
    let provinceId = $('#edit_province').val()
    let cityId = $('#edit_city').val()
    let areaId = $('#edit_area').val()
    let addr = $('#edit_addr').val()
    let stuMobile = $('#edit_stuMobile').val()
    if (stuName == null || stuName == '') {
        layer.msg('收件人姓名不能为空')
        return
    }
    if (provinceId == null || provinceId == '') {
        layer.msg('所属省份不能为空')
        return
    }
    if (cityId == null || cityId == '') {
        layer.msg('所属城市不能为空')
        return
    }
    if (areaId == null || areaId == '') {
        layer.msg('所属区县不能为空')
        return
    }
    if (addr == null || addr == '') {
        layer.msg('详细地址不能为空')
        return
    }
    if (stuMobile == null || stuMobile == '') {
        layer.msg('手机不能为空')
        return
    }
    let address = {
        receiver: stuName,
        addr: addr,
        mobile: stuMobile,
        provinceId: provinceId,
        cityId: cityId,
        areaId: areaId,
    }
    post(api.UpdateAddress, address).then(res => {
        location.reload()
    })
}

/**
 * 删除地址
 */
function deleteSingle() {
    let id = $('#hidden_id').val()
    layer.confirm('确认要删除该地址吗？删除后无法恢复。', {title: '警告框'}, function (index) {
        post(api.DeleteAddress, {id: id}).then(res => {
            location.reload()
        })
    })
}

/**
 * 提交订单到后台，微信支付
 */
function createOrder() {
    location.href = api.CreateOrder + '?productId=' + $('#productId').val() + '&carId=' + $('#hidden_car').val()
}